/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unipiloto.edu.co.generador_reportes.service.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.osgi.framework.BundleContext;
import unipiloto.edu.co.generador_reportes.service.tracker.ServiceLocator;

/**
 *
 * @author David
 */
@WebServlet(name="ListAllReportsServlet", urlPatterns={"/listAllReports"})
public class ListAllReportsServlet extends HttpServlet {
   
    private static final Logger logger = Logger.getLogger("unipiloto.edu.co");

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ServiceLocator locator = new ServiceLocator(getBundleContext());

        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Reportes Registrados: Generador de Reportes</title>");
             out.println("<style type=\"text/css\">");
            out.println("@import url(\"css/style.css\");");
            out.println("</style>");
            out.println("</head>");
            out.println("<body>");
            out.println("</br>");
            out.println("<h2 id=\"texto\">Reportes disponibles en el componente</h2>");
            out.println("<div align=center>");
            out.println("<table border=0 id=box-table-a>");
            out.println("<thead>");
            out.println("<tr>");
            out.println("<th>Reporte</th>");
            out.println("<th>Path</th>");
            out.println("</tr>");
            out.println("</thead>");
            out.println("<tbody>");
            for(String reporte:locator.getReportGeneratorService(-1).listReports()){
                out.println("<tr>");
                for(String part:reporte.split("&")){
                    out.println("<td>"+part+"</td>");
                }
                out.println("</tr>");
            }
            out.println("</tbody>");
            out.println("</table>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");

        } catch (ServiceLocator.ServiceUnavailableException e) {
            logger.log(Level.SEVERE, "El servicio no se encuentra disponible...", e);
            out.println("El servicio no se encuentra disponible...");
        } finally {
            out.close();
        }
    }

    private BundleContext getBundleContext() {
        return BundleContext.class.cast(getServletConfig().getServletContext().getAttribute("osgi-bundlecontext"));
    }
}
